package org.example.packagea.aspect;


import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.example.packagea.service.IParentService;
import org.example.packagea.service.IService;
import org.example.packagea.service.ParentServiceImpl;
import org.springframework.stereotype.Component;

@Aspect
//@Component
public class MyAspect {


    /**
     * 获取方法参数
     * @param param
     */
    @Before("execution(* org.example.packagea.service.*.*(..)) && args(param,..)")
    public void before1(Object param){
        System.out.println("advice get the param: " + param);
    }

    @Before("@annotation(annotation)")
    public void before2(HahMen annotation){
        System.out.println("advice get the annotation: " + annotation.name());
    }

    @Before("this(service)")
    public void before3(IService service){
        System.out.println("advice get the bean proxy: " + service.getClass());
    }

    @Before("target(service)")
    public void before4(IService service){
        System.out.println("advice get the bean target class: " + service.getClass());
    }

    /**
     * 泛型参数类型是Integer 才拦截
     * @param param
     */
    @Before("execution(* *.zero(..)) && args(param)")
    public void before5(Integer param){
        System.out.println("advice get the integer type: " + param);
    }



    @DeclareParents(value = "org.example.packagea.service.*",defaultImpl = ParentServiceImpl.class)
    private IParentService parentService;
    /**
     * 这里IService的bean会被当作IParentService类型的对象来使用(结合@DeclareParents注解)
     * @param parentService
     */
    @Before("bean(myServiceImpl) && this(parentService)")
    public void before6(IParentService parentService){
        parentService.doSomeThing();
    }
}
